package com.yjetms.controller;

import com.yjetms.entity.Data;
import com.yjetms.entity.Stuffbasic;
import com.yjetms.entity.User;
import com.yjetms.repository.StuffRepository;
import com.yjetms.service.StuffbasicService;
import com.yjetms.service.StuffbasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/data")
public class StuffData {

    @Autowired
    StuffRepository stuffRepository;

    //传输员工数据
    @RequestMapping("/stuffdata")
    public Data<Stuffbasic> stuffData(Integer page, Integer limit, HttpServletRequest request){

        //List<Stuffbasic> stufflist = stuffRepository.findAll();

        String stuName=request.getParameter("stuffname");
        String stuSex=request.getParameter("sex");
        String stuEdu=request.getParameter("edu");
        String stuMarr=request.getParameter("marriage");
        String stuWorklenght=request.getParameter("worklenght");
        String searchway=request.getParameter("searchway");

       System.out.println(page);
        System.out.println(limit);
        System.out.println(stuName);
        System.out.println(stuSex);
        System.out.println(stuEdu);
        System.out.println(stuMarr);
        System.out.println(stuWorklenght);
        System.out.println(searchway);



        if(searchway==null){
            System.out.println("进入无查询");
            int a = Math.toIntExact(stuffRepository.count());
            PageRequest pageRequest=PageRequest.of(page-1,limit);
            Page<Stuffbasic> page1=stuffRepository.findAll(pageRequest);
            List<Stuffbasic> stufflist=page1.getContent();
            Data<Stuffbasic> stuff_data= new Data<>(0,"",a,stufflist);
            return stuff_data;
        }else{
            Stuffbasic stuff=new Stuffbasic();
            if(stuName!=null&&!stuName.equals("")){
                System.out.println("进入了姓名赋值");
                stuff.setStuffname(stuName);
            }
            if(stuSex!=null&&!stuSex.equals("-1")){
                System.out.println("进入了性别赋值");
                stuff.setSex(stuSex);
            }
            if(stuMarr!=null&&!stuMarr.equals("-1")){
                System.out.println("进入了marr赋值"+stuMarr);
                stuff.setMerriage(stuMarr);
            }
            if(stuWorklenght!=null&&!stuWorklenght.equals("0")){
                System.out.println("进入了工作时长赋值");
                stuff.setWorklength(stuWorklenght);
            }
            if(stuEdu!=null&&!stuEdu.equals("0")){
                System.out.println("进入了教育水平时长赋值");
                stuff.setEducationlevel(stuEdu);
            }
            System.out.println("进入查询");
            System.out.println(stuff);
            StuffbasicService stuService=new StuffbasicServiceImpl(stuffRepository);
            Data<Stuffbasic> searchpage = stuService.searchpage(stuff, page, limit);
            return searchpage;
        }
    }

    @RequestMapping("/dele")
    public int deleteStuff(HttpServletRequest request){
        System.out.println(request.toString());
        int id = Integer.parseInt(request.getParameter("stuffId"));
        stuffRepository.deleteById(id);
        return 200;
    }
}
